package com.system.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.BufferedReader;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;

import static com.system.service.PrinterServiceImpl.executeCommand;

@Configuration
public class RabbitMQConfig {
    // 定义交换机和队列的常量
    private final String filePath = "./sn.txt";

    public static final String Printer_EXCHANGE ="Printer_EXCHANGE";
//    public static final String PRINTER_QUEUE = "Printer_QUEUE";
    public static final String PRINTER_QUEUE_TEST = "Printer_QUEUE_Test";
//    public static final String PRINTER_EMAIL_ROUTING_KEY = "inform.#.PrinterEmail.#";
//    public static final String PRINTER_C_QUEUE = "PrinterCQUEUE";
//    @Value("${spring.application.name}")
//    private String appName;

    // 定义打印机服务名称和队列名称
    private String printerServiceNameKey;
    private String printerQueue;

    public RabbitMQConfig() {
        // 构造函数保持空，实际初始化在serialNumber bean创建后进行
    }

    @Bean
    public String serialNumber() throws IOException {
        String serialNumber = getSerialNumber();
        // 初始化队列名称相关变量
        this.printerServiceNameKey = serialNumber;
        this.printerQueue = serialNumber + "_QUEUE";
        return serialNumber;
    }

    private String getSerialNumber() throws IOException {
        // 实现获取 serialNumber 的逻辑

        OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
        System.out.println("Operating System: " + osBean.getName());
        System.out.println("Available Processors: " + osBean.getAvailableProcessors());

        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("win")) {
            // Windows 系统，使用 wmic 命令
            try {
                String wmicBiosGetSerialnumber = executeCommand("wmic bios get serialnumber").trim();
                //序列号
                if (StrUtil.isBlank(wmicBiosGetSerialnumber)) {
                    boolean exist = FileUtil.exist(filePath);
                    if (!exist) {
                        FileUtil.writeUtf8String(UUID.fastUUID().toString(true), filePath);
                    }
                    //读取sn.txt内的序列号,使用Hutool
                    BufferedReader reader = FileUtil.getReader(filePath, "utf-8");
                    wmicBiosGetSerialnumber = reader.readLine();
                }
                return "Printer."+wmicBiosGetSerialnumber;
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            // macOS 系统，使用其他命令
            try {

                return "Printer."+executeCommand("system_profiler SPHardwareDataType | grep \"Serial Number\"").trim();
                // 处理命令输出
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }

    @Bean
    public TopicExchange printerExchange() {
        return new TopicExchange(Printer_EXCHANGE);
    }

    @Bean
    public Queue customPrinterQueue() {
        return new Queue(printerQueue,true);
    }

    // 修改参数类型为 TopicExchange
    @Bean
    public Binding bindCustomPrinterQueueToExchange(Queue customPrinterQueue, TopicExchange printerExchange) {
        return BindingBuilder.bind(customPrinterQueue).to(printerExchange).with(printerServiceNameKey);
    }
}